<template>
    <div>
        <el-form :model="loginForm" :rules="loginRules">
            <h2>计算机学院实验室约课管理系统</h2>
            <el-form-item prop="id">
                <el-input v-model="loginForm.id" type="text" auto-complete="on" placeholder="工号" ></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="loginForm.name" type="text" auto-complete="off" placeholder="姓名"
                    @keyup.enter.native="handleLogin">
                </el-input>
            </el-form-item>

            <el-form-item style="width:100%;">
                <el-button :loading="loading" size="default" type="primary" style="width:100%;"
                    @click.native.prevent="handleLogin">
                    <span v-if="!loading">登 录</span>
                    <span v-else>登 录 中...</span>
                </el-button>
                <div style="float: right;">
                    <router-link class="link-type" :to="'/register'">立即注册</router-link>
                </div>
            </el-form-item>
        </el-form>
  
    </div>
</template>

<script setup>
import { ref, reactive,  onMounted } from 'vue'
import { userApi } from '@/api/user'
import { useRoute,useRouter } from 'vue-router'
const loginForm = reactive({
    id: '105116',
    name: '王亮'
})
const loginRules = reactive({
    id: [
        { required: true, trigger: "blur", message: "请输入您的工号" }
    ],
    name: [
        { required: true, trigger: "blur", message: "请输入您的姓名" }
    ]
})
const loading = ref(false)
// const router =useRouter()
const route = useRoute()
onMounted(() => {
    // const currentUser = JSON.stringify(localStorage.getItem('currentUser'))
    // if (currentUser) {
    //     loginForm.id = '105116'//currentUser['jobNumber']
    //     loginForm.name ='王亮'// currentUser['name']
    // }

    // console.log('mounted login', route.params)

   
})

const router = useRouter()
const handleLogin = () => {
    loading.val = true
    userApi.login(loginForm.id, loginForm.name)
        .then((v) => {
            if (route.params.path) {
                console.log('route,params:',route.params)
                const p = '/' + route.params.path
                router.push({ path: p }).catch(() => {
                    console.log("xxxxredirect")
                    router.push({ name: 'home' })
                })
            } else {
                route.push({ name:'home' })
            }
        }).catch((v) => {
            console.log("login errorr:",v)
            router.push({ name:'home' })
        })

}



</script>
